package com.dyrj.question.service.impl;

import com.dyrj.question.bean.PDayanswer;
import com.dyrj.question.bean.PDayaward;
import com.dyrj.question.bean.PPointrule;
import com.dyrj.question.bean.PPrizerule;
import com.dyrj.question.dao.PDayanswerMapper;
import com.dyrj.question.dao.PPointruleMapper;
import com.dyrj.question.service.IPDayanswerService;
import com.dyrj.utils.DateUtil;
import com.dyrj.utils.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.dyrj.base.system.user.model.SysUser;
import com.dyrj.component.properties.UploadProperties;
import com.dyrj.constant.SysFrameConst;
import com.dyrj.utils.UUIDUtil;
import com.dyrj.vo.PageResultMsg;
import com.dyrj.vo.ResultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 日答题管理 服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-04-30
 */
@Service
@Log4j2
public class PDayanswerServiceImpl implements IPDayanswerService {

    @Resource
    private PDayanswerMapper mapper;
    @Resource
    private PPointruleMapper pPointruleMapper;
    @Resource
    private UploadProperties uploadProperties;
    @Value("${string.maxprize}")
    private BigDecimal maxprize;
    @Value("${string.minprize}")
    private BigDecimal minprize;
    @Value("${string.nd}")
    private String nd;
    @Override
    public PageResultMsg table(PDayanswer record) {
        PageInfo<PDayanswer> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.selectList(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }
    @Override
    public PageResultMsg table1(PDayanswer record) {
//        System.out.println(record.getOffset()+"========================="+record.getLimit()+record.getNy()+"==="+record.getUid());
        PageInfo<PDayanswer> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.getPersonRankList(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }
    @Override
    public PageResultMsg tableHj(PDayanswer record) {
//        System.out.println(record.getOffset()+"========================="+record.getLimit()+record.getNy()+"==="+record.getUid());
        PageInfo<PDayanswer> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.getPersonRankHjList(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }
    @Override
    public PageResultMsg table2(PDayanswer record) {
//        System.out.println(record.getOffset()+"========================="+record.getLimit()+record.getNy()+"==="+record.getUid());
        PageInfo<HashMap> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.getDttjList1(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }
    @Override
    public PageResultMsg table3(PDayanswer record) {
//        System.out.println(record.getOffset()+"========================="+record.getLimit()+record.getNy()+"==="+record.getUid());
        PageInfo<HashMap> list = PageHelper.startPage(record.getOffset(), record.getLimit()).doSelectPageInfo(() -> mapper.getDttjList2(record));
        return new PageResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, list.getTotal(), list.getList());
    }
    @Override
    public ResultMsg add(PDayanswer record) {
        String msg = "";
        try {
            mapper.insertSelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE+msg);
    }


    @Override
    public PDayanswer info(String id) {
        PDayanswer ylMmdagl = mapper.selectByPrimaryKey(Integer.parseInt(id));
        return ylMmdagl;
    }

    public ResultMsg update(PDayanswer record) {
        String msg = "";
        try {
            //PDayanswer record_db = mapper.selectByPrimaryKey(record.getId());
            mapper.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
        }
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE + msg);
    }

    @Override
    public ResultMsg delete(String id) {
        mapper.deleteByPrimaryKey(Integer.parseInt(id));
        return new ResultMsg(SysFrameConst.ResultCode.SUCCESS.CODE, SysFrameConst.ResultCode.SUCCESS.MESSAGE);
    }
    public HashMap getPointInfo(String uid, String dtid){
        HashMap retMap = null;
        List<HashMap> list = mapper.getPointList(uid,dtid,"");
        if(list.size()>0){
          retMap = list.get(0);
        }
        return retMap;
    }

    public List getPointBar(String uid, String rq,int ts){
        List<HashMap> retList = new ArrayList();
        for(int i=0;i<ts;i++){
            Date temprq = DateUtil.addDate(DateUtil.parse(rq,"yyyy-MM-dd"),Calendar.DATE,-i);
            List<HashMap> list = mapper.getPersonDayRankList(uid,DateUtil.format(temprq,"yyyy-MM-dd"));
            if(list.size()>0){
                HashMap retMap = list.get(0);
                retList.add(retMap);
            }
        }
        return retList;
    }
    //获取积分
    public BigDecimal getPoint(String uid){
        PDayanswer pDayanswer = new PDayanswer();
        pDayanswer.setKsny(DateUtil.format(DateUtil.getDate(),"yyyyMM"));
        pDayanswer.setJsny(DateUtil.format(DateUtil.getDate(),"yyyyMM"));
        pDayanswer.setUid(uid);
        List pDayanswerlist = mapper.selectList(pDayanswer);
        PPointrule pPointrule = new PPointrule();
        pPointrule.setBz(nd);
        pPointrule.setAnswercn(pDayanswerlist.size()+"");
        List<PPointrule> list = pPointruleMapper.selectList(pPointrule);
        if (list.size()>0) {
            PPointrule pPointrule1 = list.get(0);
            BigDecimal point = pPointrule1.getPoint();
            return point;
        }else{
            return new BigDecimal(0);
        }
    }

    //获取奖金
//    public BigDecimal getPoint(String uid){
//        PDayanswer pDayanswer = new PDayanswer();
//        pDayanswer.setRq(DateUtil.format(DateUtil.getDate(),"yyyy-MM-dd"));
//        pDayanswer.setUid(uid);
//        List pDayanswerlist = mapper.selectList(pDayanswer);
//        PPointrule pPointrule = new PPointrule();
//        pPointrule.setBz("2021");
//        if (pDayanswerlist.size()<=3) {
//            pPointrule.setAnswercn(pDayanswerlist.size()+"");
//        } else{
//            pPointrule.setAnswercn("4次以上");
//        }
//        List<PPointrule> list = pPointruleMapper.selectList(pPointrule);
//        if (list.size()>0) {
//            PPointrule pPointrule1 = list.get(0);
//            BigDecimal point = pPointrule1.getPoint();
//            return point;
//        }else{
//            return new BigDecimal(0);
//        }
//    }
}
